/*
 * @Description: 返回页面顶部按钮
 * @Author: YWJ 
 * @Date: 2022-04-19 23:32:35 
 * @Last Modified by: YWJ
 * @Last Modified time: 2022-05-14 10:20:33
 */

<template>
<el-collapse-transition>
<div @click="Totop" v-show="ifShow" class="me-to-top"> 
    <i class="el-icon-caret-top"></i>
</div>
</el-collapse-transition>
</template>

<script>
export default {
    data(){
        return{
            ifShow:false
        }
    },
    methods:{
        //定位到顶部
        Totop(){
        document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;
        this.ifShow = false;
        },
        //下滑到一定程度显示按钮
        needToTop() {
        let curHeight = document.documentElement.scrollTop || document.body.scrollTop;

        if (curHeight > 400) {
          this.ifShow = true;
        } else {
          this.ifShow = false;
        }

      }
    },
    mounted(){
        //等渲染完毕后对滚动条进行监听
        this.$nextTick(function () {
        window.addEventListener('scroll', this.needToTop);
      });

    }

    
}
</script>

<style>
.me-to-top {
    background-color: #fff;
    position: fixed;
    right: 300px;
    bottom: 50px;
    width: 40px;
    height: 40px;
    border-radius: 20px;
    cursor: pointer;
    transition: .3s;
    box-shadow: 0 0 6px rgba(0, 0, 0, .12);
    z-index: 5;
  }

  .me-to-top i {
    color: #00d1b2;
    display: block;
    line-height: 40px;
    text-align: center;
    font-size: 18px;
  }
</style>